package com.suxii.controller;

import com.suxii.pojo.ChatFriend;
import com.suxii.pojo.ChatUser;
import com.suxii.pojo.Result;
import com.suxii.service.FriendService;
import com.suxii.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private FriendService friendService;

    //登录操作
    @GetMapping("/user")
    public Result user(ChatUser chatUser){
        return Result.success(userService.user(chatUser));
    }

    // TODO 这是多表查询了，需要新建service层
    // 返回好友uid
    @GetMapping("/friend")
    public Result friend(ChatFriend chatFriend){
        return Result.success(friendService.friend(chatFriend));
    }

    // 返回好友uid
    @GetMapping("/get-friend")
    public Result get(ChatFriend chatFriend){
        List<Integer> uids = friendService.friend(chatFriend);
        return Result.success(userService.get(uids));
    }

    // 查询好友接口
    @GetMapping("/find-friend")
    public Result findFriend(ChatUser chatUser){
        // data为null则没有用户
        return Result.success(userService.findFriend(chatUser));
    }

    // 添加好友接口
    @GetMapping("/add-friend")
    public Result addFriend(ChatFriend chatFriend){
        if (chatFriend.getUid()==null||chatFriend.getUuid()==null){
            return Result.error("参数错误");
        }
        friendService.addFriend(chatFriend);
        return Result.success();
    }

    // 删除好友接口
    @GetMapping("/delete-friend")
    public Result deleteFriend(ChatFriend chatFriend){
        if (chatFriend.getUid()==null||chatFriend.getUuid()==null){
            return Result.error("参数错误");
        }
        friendService.deleteFriend(chatFriend);
        return Result.success();
    }

}
